package nz.example.uni.beaninfo;

import java.beans.IntrospectionException;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;
import java.lang.reflect.Method;

import nz.example.uni.Course;
import nz.example.uni.Paper;

public class CourseBeanInfo extends SimpleBeanInfo {
	private Class<Course> courseClass = Course.class;

	@Override
	public PropertyDescriptor[] getPropertyDescriptors() {
		PropertyDescriptor papers = null, name = null;

        try {
        	name = new PropertyDescriptor("courseName", courseClass);
        	name.setDisplayName("The name of the course");
        	
        	papers = new PropertyDescriptor("papers", courseClass);
        	papers.setDisplayName("The list of papers associated with the course.");

        } catch (IntrospectionException ie) {
        	ie.printStackTrace();
        }

        PropertyDescriptor[] result = {name, papers};
        return result;
	}
	
	public MethodDescriptor[] getMethodDescriptors() {

        Method add = null, remove = null;
        MethodDescriptor addPaper = null, removePaper = null;

        try {

        	add = courseClass.getMethod("addPaper", new Class[]{Paper.class});
        	remove = courseClass.getMethod("removePaper", new Class[]{Paper.class});
        	
            addPaper = new MethodDescriptor(add);
            addPaper.setDisplayName("Adds a paper to the course.");
            removePaper = new MethodDescriptor(remove);
            removePaper.setDisplayName("Removes a paper from the course.");

        } catch (NoSuchMethodException nsme) {
        	nsme.printStackTrace();
        }

        MethodDescriptor[] result = {addPaper, removePaper};
        return result;
    }

}
